package managedbean.CRM.Customer;

import entity.CRM.Customer;
import entity.CRM.Representative;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;

@ManagedBean(name="editCustomerManagerBean")
@ViewScoped

public class EditCustomerManagerBean implements Serializable
{
    @EJB
    private CustomerBeanLocal customerSessionBeanLocal;
    private Customer customerToEdit;
    private Representative selectedRep;
    List<Representative> reps;
    Representative newRep = new Representative();
    private String statusMessage;
    
    public EditCustomerManagerBean() {}
    
    public Customer getCustomerToEdit() {
        return customerToEdit;
    }
    public void setCustomerToEdit(Customer customerToEdit) {
        this.customerToEdit = customerToEdit;
    }

    public CustomerBeanLocal getCustomerSessionBeanLocal() {
        return customerSessionBeanLocal;
    }

    public void setCustomerSessionBeanLocal(CustomerBeanLocal customerSessionBeanLocal) {
        this.customerSessionBeanLocal = customerSessionBeanLocal;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Representative getNewRep() {
        return newRep;
    }

    public void setNewRep(Representative newRep) {
        this.newRep = newRep;
    }

    public List<Representative> getReps() {
        return reps;
    }

    public void setReps(List<Representative> reps) {
        this.reps = reps;
    }

    public Representative getSelectedRep() {
        return selectedRep;
    }

    public void setSelectedRep(Representative selectedRep) {
        this.selectedRep = selectedRep;
    }
    

    public void initView(PhaseEvent event)
    {
        if(customerToEdit == null)
        {            
            customerToEdit = (Customer)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedCustomer");            
            this.setReps(customerToEdit.getRepresentatives());
        }
    }
    
    public void updateExistingCustomer(ActionEvent event)
    {
        try
        { 
            customerSessionBeanLocal.updateCustomer( customerToEdit.getSaleLeadName(), customerToEdit.getRegistrationNumber(), customerToEdit.getSaleLeadAddress(), customerToEdit.getCustomerType());
            statusMessage = "Customer Updated Successfully"; 

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Customer Result: " + statusMessage + " (Customer ID is " + customerToEdit.getRegistrationNumber().toString() + ")", "")); 
        }
        catch(Exception ex){
         ex.printStackTrace();   
        }
    }    
    
    public void addRep(ActionEvent event)
    {
        
        Representative temp = this.newRep;
        reps.add(temp);
        newRep = new Representative();
    }
    
    public void editRep(ActionEvent event)
    {
        Representative temp = this.selectedRep;
        
        for (int i = 0; i < reps.size(); i++)
        {
            if (reps.get(i).getEmail() == temp.getEmail())
            {
                reps.get(i).setName(temp.getName());
                reps.get(i).setContactNumber(temp.getContactNumber());
            }
        }
        
    }
    
    public void deleteRep(ActionEvent event)
    {
        Representative temp = this.selectedRep;
        for (int i = 0; i < reps.size(); i++)
        {
            if (reps.get(i).getEmail() == temp.getEmail())
            {
                reps.remove(i);
            }
        }
    }
}
